<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Report_item extends MY_Controller {

	function Report_item() {
		parent::__construct();
		$this->load->model('report_model');
	    $this->load->model('report_item_model');
	    $this->load->model('report_group_model');
	}

	public function index() {
		$reports = $this->report_model->report_lists();
		$group_names_list  = $this->report_group_model->get_group_list();
		$groups  = $this->report_group_model->get_group_list();
		$items   = $this->report_item_model->get_all_item();
		if($this->session->userdata('logged_in') && $this->session->userdata('role_id') == 1){
			$this->load->view('report_item', array('reports' => $reports, 'items' => $items, 'groups' => $groups, 'group_names_list' => $group_names_list ));
		} else {
			$this->load->view('login');
		}
	} 

	public function select_by_groupid() { 
		$group_id  = $this->input->post('group_id');
		$all_items = $this->report_item_model->get_all_item_by_groupid($group_id);
		echo json_encode($all_items);
	}

	public function get_item_key() {
		$item_type   = $this->input->post('item_type');
		$item_server = $this->input->post('item_server');  
		$item_keys   = $this->report_item_model->get_item_keys($item_type, $item_server);
		$key_str = "";
		foreach ($item_keys as $key => $value) {
			$key_str = $key_str.$value['item_key']." ";
		}
		print_r(trim($key_str));
	}

	public function get_item_server() { 
		$from_ip = $this->input->ip_address();
		switch ($from_ip)
		{
		case "120.24.166.15":
		  $item_server = "sz";
		  break;
		case "52.68.67.135":
		  $item_server = "jp";
		  break;
		case "58.96.171.124":
		  $item_server = "hk";
		  break;
		default:
		  $item_server = "none";
		}
		echo $item_server;
	}

	public function add() {
		$item_sort    = $this->input->post('item_sort');
		$item_key     = $this->input->post('item_key');
		$item_name    = $this->input->post('item_name');
		$item_type    = $this->input->post('item_type');
		$item_server  = $this->input->post('item_server');
		$group_name   = $this->input->post('group_name');
		$row          = $this->report_group_model->get_group_by_name($group_name);
		$report_items = $this->report_item_model->get_by_name($item_key,item_server);
		if (empty($report_items)) {
			$this->report_item_model->add($item_sort, $item_key, $item_name, $item_type, $item_server, $row['group_id']);
		} else {
			echo "<script>alert('Already exist');window.history.go(-1);</script>";
		}
		redirect("/report_item");
	}

	public function delete() {
		$item_id  = $this->input->get('item_id');
		$item_ids = $this->report_item_model->get_by_id($item_id);
	    if (!empty($item_ids)) {
	    	$this->report_item_model->delete($item_id);
	    } else {
	    	echo "<script>alert('Not exist');window.history.go(-1);</script>";
	    }
	    redirect("/report_item");
	}
	
	public function batch_add() { 
		$items = $this->input->post('items');
		$newitems = trim($items,"<br>");
		$arr = explode("<br>",$newitems);
		foreach ($arr as $k => $v) {
			$item = explode(":", $v);
			$row  = $this->report_group_model->get_group_by_name($item[0]);
			$report_items = $this->report_item_model->get_by_name($item[1], $item[4]);
			if (empty($report_items)) {
				$this->report_item_model->add($item[5], $item[1], $item[2], $item[3], $item[4], $row['group_id']);
			}
		}
		redirect("/report_item");

	}

	public function update() {
		$item_id    = $this->input->post('item_id');
		$item_sort  = $this->input->post('item_sort');
		$item_key   = $this->input->post('item_key');
		$item_name  = $this->input->post('item_name');
		$item_type  = $this->input->post('item_type');
		$item_server= $this->input->post('item_server');
		$group_name = $this->input->post('group_name');
		$row = $this->report_group_model->get_group_by_name($group_name);
		$this->report_item_model->update($item_id, $item_sort, $item_key, $item_name, $item_type, $item_server, $row['group_id']);
		redirect("/report_item");
	}
	
}